package oa.hleast.xswl.service.pms.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import oa.hleast.common.result.Result;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.common.enums.CouponSourceEnum;
import oa.hleast.xswl.common.enums.PointOperatingEnum;
import oa.hleast.xswl.common.enums.PointThingEnum;
import oa.hleast.xswl.mapper.pms.PmsPointThingMapper;
import oa.hleast.xswl.pojo.domain.pms.PmsPointThing;
import oa.hleast.xswl.pojo.domain.pms.PmsUserPointThing;
import oa.hleast.xswl.service.IUmsUserService;
import oa.hleast.xswl.service.pms.PmsPointThingService;
import oa.hleast.xswl.service.pms.PmsUserCouponService;
import oa.hleast.xswl.service.pms.PmsUserPointThingService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: 00209
 * @date: 2021-06-09 16:56:45
 * @description: 积分物品表(PmsPointThing)接口实现
 */
@AllArgsConstructor
@Service("pmsPointThingService")
public class PmsPointThingServiceImpl extends ServiceImpl<PmsPointThingMapper, PmsPointThing> implements PmsPointThingService {

    private IUmsUserService memberFeignService;
    private PmsUserCouponService pmsUserCouponService;
    private PmsUserPointThingService pmsUserPointThingService;

    @Override
    @Transactional
    public Result exchange(Long userId, Long id) {
        PmsPointThing pmsPointThing = this.getById(id);
        // todo: 积分是否足够
        Long userPoint = memberFeignService.getPoint(RequestUtils.getUserId());
        if (userPoint.compareTo(pmsPointThing.getPoint()) < 0) {
            throw new BizException("会员积分不够");
        }
        // todo: 预扣积分

        Integer pointOperatingType = null;
        Boolean update = false;
        if (pmsPointThing != null) {
            PointThingEnum pointThingEnum = PointThingEnum.getValue(pmsPointThing.getType());
            switch (pointThingEnum) {
//
                case COUPON:
                    //
                    pointOperatingType = PointOperatingEnum.COUPON_EXCHANGE.getCode();
                    update = pmsUserCouponService.receive(pmsPointThing.getRelationId(), userId, CouponSourceEnum.EXCHANGE);
                    break;
            }
        }

        if (update) {
            PmsUserPointThing pmsUserPointThing = new PmsUserPointThing();
            pmsUserPointThing.setUserId(userId);
            pmsUserPointThing.setPointThingId(id);
            pmsUserPointThingService.save(pmsUserPointThing);
            // 积分扣除
            memberFeignService.updatePoint(pmsPointThing.getPoint(), pointOperatingType);
        }


        return Result.success(update);
    }

    @Override
    public PmsPointThing getPointThingById(Long relativeId) {

        LambdaQueryWrapper<PmsPointThing> lambdaQueryWrapper = new LambdaQueryWrapper<PmsPointThing>()
                .eq(PmsPointThing::getType, PointThingEnum.PRODUCT.getCode())
                .eq(PmsPointThing::getId, relativeId);
        List<PmsPointThing> list = this.list(lambdaQueryWrapper);
        if(CollectionUtils.isNotEmpty(list) && ObjectUtil.isNotNull(list.get(0))){
            return list.get(0);
        }

        return list.get(0);
    }

    @Override
    public void addPointThing(Long id) {

    }
}
